using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microformats.Entities;
using Microformats.Readers;
using NUnit.Framework;

namespace MircoformatParsingTest
{
    [TestFixture]
    public class relTagParsingTests
    {
        private const string testFolder = "rel-tag Test Files";


        /// <summary>
        /// Parses the file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        private static Stream ParseFile(string fileName)
        {
            string path = Path.Combine(testFolder, fileName);
            return new FileStream(path, FileMode.Open);
        }

        /// <summary>
        /// Parses the microformats.
        /// </summary>
        private static List<Microformat> ParseMicroformats(string fileName)
        {
            List<Microformat> outputFormats = new List<Microformat>();
            using (Stream ms = ParseFile(fileName))
            {
                using (
                    MicroformatReader mr =
                        new MicroformatReader(ms, InputSourceType.Html))
                {
                    Microformat m;
                    while ((m = mr.Read()) != null)
                    {
                        outputFormats.Add(m);
                    }
                }
            }

            return outputFormats;
        }

        /// <summary>
        /// Parses the microformats.
        /// </summary>
        private static List<Microformat> ParseMicroformats(string fileName, string baseAddress)
        {
            List<Microformat> outputFormats = new List<Microformat>();
            using (Stream ms = ParseFile(fileName))
            {
                using (
                    MicroformatReader mr =
                        new MicroformatReader(ms, InputSourceType.Html, baseAddress))
                {
                    Microformat m;
                    while ((m = mr.Read()) != null)
                    {
                        outputFormats.Add(m);
                    }
                }
            }

            return outputFormats;
        }

        [Test]
        public void SimpleRelTag_01()
        {
            List<Microformat> outputFormats = ParseMicroformats("01-simple-rel-tag.htm");

            //Ensure that the number of formats output is correct
            Assert.AreEqual(1, outputFormats.Count);
            Assert.AreEqual("Kinlan", outputFormats[0].HumanValue);
            Assert.AreEqual("http://www.topicala.com/tag/Kinlan", outputFormats[0].MachineValue);
        }

        [Test]
        public void BaseTest_02()
        {
            List<Microformat> outputFormats = ParseMicroformats("02-base-test.htm", "http://www.topicala.com/");

            //Ensure that the number of formats output is correct
            Assert.AreEqual(10, outputFormats.Count);

            for (int i = 0; i < 10; i++)
            {
                Assert.AreEqual("test" + (i+1).ToString(), outputFormats[i].HumanValue);
                Assert.AreEqual("http://www.topicala.com/tag/test" + (i + 1).ToString(), outputFormats[i].MachineValue);
            }
        }
    }
}
